/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ActionBar;

import CRUD.MessageFacadeLocal;
import CRUD.S3r4OrderFacadeLocal;
import CRUD.OrderItemFacadeLocal;
import Entity.Item;
import Entity.ItemPK;
import Entity.Message;
import Entity.MessagePK;
import Entity.OrderItem;
import Entity.OrderItemPK;
import Entity.S3r4Order;
import Entity.S3r4OrderPK;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Order;


/**
 *
 * @author WinKiQue58
 */
@Stateless
public class MessageOptions implements MessageOptionsLocal {
    
  

     @PersistenceContext(unitName = "S3R4_Orcl-ejbPU")
        private EntityManager em;
     
       @EJB private MessageFacadeLocal messageFacade;
       @EJB private S3r4OrderFacadeLocal orderFacade;
       @EJB private OrderItemFacadeLocal orderItemFacade;
       
    
     public boolean CreateMessage(String[] address, String message, String Who,
             BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
     {
         try{
         BigInteger maxID;
         Query q1= em.createQuery("SELECT MAX(m.messagePK.idMessage) FROM Message m WHERE "
                  + "m.messagePK.idPersonalInfo = :A AND "
                  + "m.messagePK.idBranchenterprise = :B AND "
                  + "m.messagePK.idEnterprise = :C");
          q1.setParameter("A", idPersonalInfo);
          q1.setParameter("B", idBranchEnterprise);
          q1.setParameter("C", idEnterprise);
         try{
         maxID=(BigInteger)q1.getSingleResult();
         if (maxID==null)  maxID=new BigInteger("1");
         } catch(NoResultException e) {
          maxID=new BigInteger("1");
         }
         
         MessagePK mPK=new MessagePK(maxID.add(new BigInteger("1")),idPersonalInfo , idBranchEnterprise, idEnterprise);
         
         String TO="";
         for (int i = 0; i < address.length; i++) {
            TO+= address[i]+", ";
         }
         
         Message m=new Message(mPK,TO, message, Who);
         messageFacade.create(m);
         }catch (Exception e)
         { return false; }
         return true;
     }
     
     public boolean CreateOrder (BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
     {
         BigInteger maxID;
                  
         try{
         Query q1= em.createQuery("SELECT MAX(o.s3r4OrderPK.idOrder) FROM S3r4Order o WHERE "
                  + "o.s3r4OrderPK.idPersonalInfo = :A AND "
                  + "o.s3r4OrderPK.idBranchenterprise = :B AND "
                  + "o.s3r4OrderPK.idEnterprise = :C");
          q1.setParameter("A", idPersonalInfo);
          q1.setParameter("B", idBranchEnterprise);
          q1.setParameter("C", idEnterprise);
          
           maxID=(BigInteger) q1.getSingleResult();
          if (maxID==null)  maxID=new BigInteger("1");
         } catch(NoResultException e) {
          maxID=new BigInteger("1");
         }
         
         S3r4OrderPK oPK=new S3r4OrderPK(maxID.add(new BigInteger("1")), idPersonalInfo, idBranchEnterprise, idEnterprise);
         S3r4Order o=new S3r4Order(oPK);
         orderFacade.create(o);
         return true;
     }
     
     
      public boolean CreateOrderItem (Item i, 
              BigInteger idOrder, BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
     {
         BigInteger maxID;
                  
         try{
         Query q1= em.createQuery("SELECT MAX(oi.orderItemPK.idOrderItem) FROM OrderItem oi WHERE "
                  + "oi.orderItemPK.idOrder = :A AND "
                  + "oi.orderItemPK.idPersonalInfo = :B AND "
                  + "oi.orderItemPK.idBranchenterprise = :C AND "
                  + "oi.orderItemPK.idEnterprise = :D"); 
          q1.setParameter("A", idOrder);
          q1.setParameter("B", idPersonalInfo);
          q1.setParameter("C", idBranchEnterprise);
          q1.setParameter("D", idEnterprise);
          
           maxID=(BigInteger) q1.getSingleResult();
          if (maxID==null)  maxID=new BigInteger("1");
         } catch(NoResultException e) {
          maxID=new BigInteger("1");
         }
                     
         OrderItemPK oiPK=new OrderItemPK(maxID.add(new BigInteger("1")),idOrder, idPersonalInfo, idBranchEnterprise, idEnterprise);
         OrderItem oi=new OrderItem(oiPK,i);
         orderItemFacade.create(oi);
         return true;
     }
      
      
     public boolean CloseOrder (BigInteger idOrder, BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
     {
         
         Map <String, String> m=new HashMap<String, String>();
                 
         Query q2= em.createQuery("SELECT o FROM S3r4Order o WHERE "
                 + "o.s3r4OrderPK.idOrder = :A AND "
                 + "o.s3r4OrderPK.idPersonalInfo = :B AND "
                 + "o.s3r4OrderPK.idBranchenterprise = :C AND "
                 + "o.s3r4OrderPK.idEnterprise = :D");
         q2.setParameter("A", idOrder);
         q2.setParameter("B",idPersonalInfo );
         q2.setParameter("C",idBranchEnterprise );
         q2.setParameter ("D", idEnterprise);
         
         S3r4Order o= (S3r4Order)q2.getSingleResult();
         List<OrderItem> list=o.getOrderItemList();
         for (OrderItem orderItem : list) {
             if (m.get(orderItem.getItem().getProduct().getName())==null)
             m.put(orderItem.getItem().getProduct().getName(), orderItem.toString());
         }
         
         
         o.setNumitems(list.size());
         o.setNumproducts(m.size());
         
                 
         return true;
     } 
     
     //--Helper Method
     public Item GetItem(BigInteger idItem, BigInteger idProduct, 
             BigInteger idBranchenterprise, BigInteger idEnterprise)
     {
          Query q1= em.createQuery("SELECT i FROM Item i WHERE "
                  + "i.itemPK.idItem = :A AND "
                  + "i.itemPK.idProduct = :B AND "
                  + "i.itemPK.idBranchenterprise = :C AND "
                  + "i.itemPK.idEnterprise = :D");
          q1.setParameter("A", idItem);
          q1.setParameter("B", idProduct);
          q1.setParameter("C", idBranchenterprise);
          q1.setParameter("D", idEnterprise);
          
          Item i;
          try{
          i=(Item)q1.getSingleResult();
          } catch(NoResultException e) 
          {
              return null;
          }
         
          return i;
     }
     
     public List<S3r4Order> getOrders (BigInteger idPersonalInfo, BigInteger idBranchEnterprise,
             BigInteger idEnterprise)
     {
        Query q2= em.createQuery("SELECT o FROM S3r4Order o WHERE "
                 + "o.s3r4OrderPK.idPersonalInfo = :B AND "
                 + "o.s3r4OrderPK.idBranchenterprise = :C AND "
                 + "o.s3r4OrderPK.idEnterprise = :D");
         q2.setParameter("B",idPersonalInfo );
         q2.setParameter("C",idBranchEnterprise );
         q2.setParameter ("D", idEnterprise);
         
         List<S3r4Order> list;
        try{ 
        list= (List<S3r4Order>)q2.getResultList(); 
        }catch(NoResultException e) {
          return null;
         }
        return list;
     }  
     
     public List<Message> getMessages (BigInteger idPersonalInfo, BigInteger idBranchEnterprise,
             BigInteger idEnterprise)
     {
        Query q2= em.createQuery("SELECT m FROM Message m WHERE "
                 + "m.messagePK.idPersonalInfo = :B AND "
                 + "m.messagePK.idBranchenterprise = :C AND "
                 + "m.messagePK.idEnterprise = :D");
         q2.setParameter("B",idPersonalInfo );
         q2.setParameter("C",idBranchEnterprise );
         q2.setParameter ("D", idEnterprise);
         
         List<Message> list;
        try{ 
        list= (List<Message>)q2.getResultList(); 
        }catch(NoResultException e) {
          return null;
         }
        return list;
     } 
}
